package com.atguigu.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.common.constant.ProductConstant;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    AttrAttrgroupRelationServiceImpl relationService;
    @Autowired
    AttrAttrgroupRelationDao attrgroupRelationDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    AttrGroupDao groupDao;
    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void save(AttrVo attrVo) {
        // 保存基本属性消息
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attrVo,attrEntity);
        this.save(attrEntity);
        // 保存关联的信息
        if(attrVo.getAttrType()==ProductConstant.AttrEnum.Attr_TYPE_BASE.getCode()){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
            relationService.save(relationEntity);
        }


    }

    @Override
    public PageUtils baseList(Map<String, Object> params, Long catelogId, String type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equals(type)? ProductConstant.AttrEnum.Attr_TYPE_BASE.getCode() :
                ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        if(catelogId!=0){
            wrapper.eq("catelog_id",catelogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((item)->{
                item.like("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> list = (List<AttrEntity>) pageUtils.getList();
        List<AttrRespVo> respVos = list.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtil.copyProperties(attrEntity, attrRespVo);
            if("base".equals(type)){
                AttrAttrgroupRelationEntity relationEntity = attrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (relationEntity != null) {
                    Long groupId = relationEntity.getAttrGroupId();
                    AttrGroupEntity attrGroupEntity = groupDao.selectById(groupId);
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }

            CategoryEntity category = categoryDao.selectById(attrEntity.getCatelogId());
            if (category != null) {
                attrRespVo.setCatelogName(category.getName());
            }
            return attrRespVo;

        }).collect(Collectors.toList());
        pageUtils.setList(respVos);
        return pageUtils;
    }

    @Override
    public AttrRespVo getAttrDetail(Long attrId) {
        AttrRespVo vo = new AttrRespVo();
        AttrEntity attrEntity = this.getById(attrId);
        BeanUtil.copyProperties(attrEntity,vo);
        if (attrEntity!=null){
            AttrAttrgroupRelationEntity relationEntity = attrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            if(relationEntity!=null){
                // 分組信息
                if(attrEntity.getAttrType()==ProductConstant.AttrEnum.Attr_TYPE_BASE.getCode()){
                    AttrGroupEntity attrGroupEntity = groupDao.selectById(relationEntity.getAttrGroupId());
                    if(attrGroupEntity!=null){
                        vo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
                        vo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }

                // 分類信息
                Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
                vo.setCatelogPath(catelogPath);
                CategoryEntity category = categoryDao.selectById(attrEntity.getAttrId());
                if(category!=null){
                    vo.setCatelogName(category.getName());
                }
            }
        }

        return vo;
    }

    @Override
    @Transactional
    public void updateAttr(AttrVo attr) {
        // 1.修改基本属性信息
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);
        // 2.修改关联关系
        if(attrEntity.getAttrType()==ProductConstant.AttrEnum.Attr_TYPE_BASE.getCode()){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            Integer attr_id = attrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
            if(attr_id!=0){
                relationEntity.setAttrId(attr.getAttrId());
                relationEntity.setAttrGroupId(attr.getAttrGroupId());
                attrgroupRelationDao.update(relationEntity,new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
            }else {
                relationEntity.setAttrId(attr.getAttrId());
                relationEntity.setAttrGroupId(attr.getAttrGroupId());
                attrgroupRelationDao.insert(relationEntity);
            }
        }



    }

    @Override
    public List<AttrEntity> getAttrList(Long attrgroupId) {
        // 先从关联表中获取所有的属性id
        List<AttrAttrgroupRelationEntity> groupList = attrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        if(!CollectionUtils.isEmpty(groupList)){
            List<Long> attrIds = groupList.stream().map(group -> {
                Long attrId = group.getAttrId();
                return attrId;
            }).collect(Collectors.toList());
            List<AttrEntity> attrEntities = (List<AttrEntity>) this.listByIds(attrIds);
            return attrEntities;

        }
        return null;
    }

}